package com.lipeng.api;

import com.lipeng.bean.Collect;
import com.lipeng.bean.Shop;
import com.lipeng.service.ShopService;
import com.lipeng.utils.AjaxResult;
import com.lipeng.utils.IPUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by dllo on 18/3/16.
 */
@Controller
@ResponseBody
@RequestMapping(value = "/api/")
public class ShopControllerAPI {
    @Resource
    private ShopService shopService;

    //商品列表
    @RequestMapping(value = "allshop")
    public AjaxResult allShop(){
        AjaxResult result = new AjaxResult();
        List<Shop> shopList = shopService.selectAllShop();
        if (shopList == null){
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }
        for (Shop shop : shopList){
            shop.setSpic(IPUtils.getIP()+":8080"+shop.getSpic());
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(shopList);
        return result;
    }

    //商品详情
    @RequestMapping(value = "shopdetail")
    public AjaxResult shopDesc(@RequestParam("sid")Integer sid,@RequestParam("uid")Integer uid){
        AjaxResult result = new AjaxResult();
        Shop shop = shopService.selectShopById(sid);
        if (shop == null){
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }
        boolean isCollect = true;
        List<Collect> collectList = shopService.findCollect(sid,uid);
        System.out.println(collectList);
        if (collectList.isEmpty()){
            isCollect = false;
        }
        shop.setIsCollect(isCollect);
        shop.setSpic(IPUtils.getIP()+":8080"+shop.getSpic());
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(shop);
        return result;
    }



    //添加收藏
    @RequestMapping(value = "addcollect")
    public AjaxResult collect(@RequestParam("uid")Integer uid,@RequestParam("sid") Integer sid){
        return shopService.addCollect(uid,sid);
    }

    //取消收藏
    @RequestMapping(value = "delcollect")
    public AjaxResult delCollect(@RequestParam("uid")Integer uid,@RequestParam("sid")Integer sid){
        return shopService.delCollect(uid,sid);
    }

    //购买
    @RequestMapping(value = "addorder")
    public AjaxResult addOrder(@RequestParam("uid")Integer uid,@RequestParam("sid")Integer sid,@RequestParam("count")Integer count){
        return shopService.addOrder(uid,sid,count);
    }

    //取消订单
    @RequestMapping(value = "delorder")
    public AjaxResult delOrder(@RequestParam("oid")Integer oid){
        return shopService.delOrder(oid);
    }

    //付款
    @RequestMapping(value = "payorder")
    public AjaxResult payOrder(@RequestParam("oid")Integer oid){
        return shopService.payOrder(oid);
    }

    //根据名称搜索
    @RequestMapping(value = "searchshopname")
    public AjaxResult searchname(@Param("name")String name){
        AjaxResult result = new AjaxResult();
        List<Shop> shopList = shopService.selectShopByName(name);
        if (shopList==null){
            result.setCode("0");
            result.setMessage("无此商品");
            return result;
        }
        for (Shop shop : shopList){
            shop.setSpic(IPUtils.getIP()+":8080"+shop.getSpic());
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(shopList);
        return result;
    }

}
